var _account_servcie = require('service/account-service.js');
require('./index.css');

var header = {
    init : function() {
        
        this.bindEvents();
        this.loadAccountInfo();
        this.upOrDown();
        this.cartDown();
        return this; 
    },
    bindEvents : function() {
        $('#signin').click(
            function () {
                window.location.href = './account-login.html?redirect='+encodeURIComponent(window.location.href);
            }
        ); 
        $('#signOn').click (
          function () {
            if (confirm("确定要退出吗？")) {
                _account_servcie.signOn (
                    function (res) {
                            window.location.href = './catalog-main.html';
                            //console.log(res);
                    },
                    function (errorMsg) {
                        console.log(errorMsg);
                    }
                )
            }
         }
        );
        $('#myAccount').click (
            function () {
                _account_servcie.checkLogin(
                    function(res) {
                        _account_servcie.myAccount(
                            function(res) {
                                window.location.href = './account-myAccount.html';
                                //console.log('myAccouont...');
                            },
                            function(errMsg) {
                                //console.log('error_myAccount...');
                            }
                        ); 
                    },
                    function(errMsg) {
                        console.log('weidenglu...');

                    }
                );
            }
        );
    },
    upOrDown : function(){
        var a = document.getElementById("Logoa"); 
        a.href = "./catalog-main.html";
    },
    cartDown : function (){
        var b = document.getElementById("viewCart"); 
        _account_servcie.checkLogin(
            function(res) {
                _account_servcie.myAccount(
                    function(res) {
                        b.href = "./cart-myCart.html?id=myCart";
                    },
                    function(errMsg) {
                        console.log('error_mycart...');
                    }
                );                   
            },
            function(errMsg) {
                console.log('weidenglu...');
            }
        )
    },
    // 用户鉴权信息的加载
    loadAccountInfo : function() {
        _account_servcie.checkLogin(
            function(res) {
                // 对应Thymeleaf中账号已登录的相关逻辑
                console.log('Success...');
                $('#signin').remove();
                $('#signOn').show();    
                $('#myAccount').show();            
            },
            function(errMsg) {
                // 对应Thymeleaf中账号未登录的相关逻辑
                console.log('Error...');
                $('#signOn').hide();
                $('#signin').show();  
                $('#myAccount').hide();
            }
        );
    },
};

module.exports = header.init();